#!/usr/bin/env python
# coding=utf-8
import requests

import xlsxwriter as xw

def test(index=1):
    url = f"http://beta.erp.starlinke.cn/api/v1.0/products/product/page-by-product?pageIndex={index}&pageSize=10&originSourceType=&sourceType=&supplierName=&categoryId=&minTotalPrice=&maxTotalPrice=&supplierSkuCode=&minInventoryQuantity=&maxInventoryQuantity=&offlineReason=&productMetadataJson=%5B%5D"
    headers = {"authorization":"eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJjaGVuamlhbmJvMDMiLCJ1c2VySWQiOiI5MTgiLCJuYW1lIjoiY2hlbmppYW5ibzAzIiwic3VwZXJBZG1pbiI6dHJ1ZSwiZ3JvdXBBZG1pbiI6dHJ1ZSwicm9sZUlkSnNvbiI6IlsxLCAyLCA3LCA4XSIsImV4cCI6MTYyMjYyODM5MX0.J8kc5xbsSbKgJXBZdg03r9NGzM3hgLKM8puwQ-MrvCvU6Doabq6TMxS3N1fAbiTTO-zore4VVbqNzEARwNGrEAUtFuXMNUq56L8A0gLwkC90xCZRTufYxeh7RDzMb5lR6ptaHxmzJ5yQPhRphdQ3zckeQaiUw0v6dgW8WQH5N9GhzEF1pZ_py6SCiEAi-eom6L46luq8s4H1s7RqY8gOWbNTjkhJkzpHTqzH7kEHAcVZY7KuO-NikHgXRq7lGKzKz4K7zVTFShs-NJl1r5GxqAn083A4ZppFlJ-IZ2sVHvRsScSyVv3ZERx_MZH2FvSZym9UmXujKEWC22nW8miWEQ","content-type":"application/json;charset=UTF-8"}
    r = requests.get(url=url,headers=headers)
    return r.json().get("data").get("list")


from xlwt import *

def xw_toExcel(fileName):  # xlsxwriter库储存数据到excel
    workbook = xw.Workbook(fileName)  # 创建工作簿
    worksheet1 = workbook.add_worksheet("sheet1")  # 创建子表
    worksheet1.activate()  # 激活表
    title = ['*SKU', '*供应商ID', '是否默认供应商', '采购比例-系统', '采购比例-供应商', '*采购价', '*供应商SKU', '交期']  # 设置表头
    worksheet1.write_row('A1', title)  # 从A1单元格开始写入表头
    i = 2  # 从第二行开始写入数据
    for j in range(1, 1400):
        value = test(j)
        for sku_info in value:
            insertData = [sku_info['productVariantList'][0]['skuCode'], 22899, '是','','',1,'','']
            row = 'A' + str(i)
            worksheet1.write_row(row, insertData)
            i += 1
            print(i)
    workbook.close()  # 关闭表

xw_toExcel("C:\\Users\\Administrator\\Desktop\\供应关系（线下）批量导入模块.xlsx")